<?php
/**
 * 广告组件
 * @author ycoe
 */
class AdWidget extends YWidget{
	/**
	 * 广告代码
	 * @var string
	 */
	public $code = null;
	public $thumb = '200x200';
	public $count = 5;
	public $title = null;
	
	private $ad = null;
	public function init(){
		if(empty($this->code)){
			return;
		}
		//加载组件资源
		$this->load_resource();
		
		$mod = new AdMondel();
		$condition = array(
			'site_id' 	=> SITE_ID,
			'code'		=> $this->code,
		);
		$this->ad = $mod->get_by_attrs($condition);
	}
	
	public function run(){
		if(empty($this->ad)){
			return;
		}
		if(!isset($this->ad['items'])){
			return;
		}
		$now = time();
		$items = array();
		$item_count = 0;
		foreach ($this->ad['items'] as $item){
			if(isset($item['start'])){
				if($now < $item['start']){
					continue;
				}
			}
			if(isset($item['end'])){
				if($now > $item['end']){
					continue;
				}
			}
			++$item_count;
			$items[] = $item;
			if($item_count >= $this->count){
				break;
			}
		}
		if(empty($items)){
			return;
		}
		
		$this->render($this->class_name . '/' . $this->tpl, array(
			'id'	=> $this->id,
			'ad' 	=> $this->ad,
			'items'	=> $items,
			'thumb' => $this->thumb,
			'title'	=> $this->title,
		));
	}
}

?>